package proj2501.simple_module;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.OverlayLayout;
import javax.swing.SpringLayout;

public class P014Layout extends JPanel {
	private JSplitPane splitPane;
	private JPanel dummyPanel = new JPanel();
	public P014Layout() {
		System.out.println("Java Swing layout samples.");
		setLayout(new BorderLayout());
		splitPane = new JSplitPane();
		LayoutList layoutList = new LayoutList();
		JScrollPane layoutListScroll = new JScrollPane(layoutList);
		
		splitPane.setLeftComponent(layoutListScroll);
		splitPane.setRightComponent(dummyPanel);
		add(splitPane);
	}
	
	private boolean open(String string) {
		JPanel panel;
		if (string.equals("BorderLayout")) {
			panel = new BorderLayoutSample();
		} else if (string.equals("BoxLayout")) {
			panel = new BoxLayoutSample();
		} else if (string.equals("CardLayout")) {
			panel = new CardLayoutSample();
		} else if (string.equals("FlowLayout")) {
			panel = new FlowLayoutSample();
		} else if (string.equals("GridBagLayout")) {
			panel = new GridBagLayoutSample();
		} else if (string.equals("GridLayout")) {
			panel = new GridLayoutSample();
		} else if (string.equals("GroupLayout")) {
			panel = new GroupLayoutSample();
		} else if (string.equals("OverlayLayout")) {
			panel = new OverlayLayoutSample();
		} else if (string.equals("SpringLayout")) {
			panel = new SpringLayoutSample();
		} else {
			JOptionPane.showMessageDialog(this, "Not implemented.");
			return false;
		}
		splitPane.setRightComponent(panel);
		return true;
	}
	
	private class LayoutList extends JList<String> {
		public LayoutList() {
			setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			DefaultListModel<String> model = new DefaultListModel<String>();
			model.addElement("BorderLayout");
			model.addElement("BoxLayout");
			model.addElement("CardLayout");
			model.addElement("FlowLayout");
			model.addElement("GridBagLayout");
			model.addElement("GridLayout");
			model.addElement("GroupLayout");
			model.addElement("OverlayLayout");
			model.addElement("SpringLayout");
			setModel(model);
			addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					Object source = e.getSource();
					JList<?> list = (JList<?>) source;
					int index = list.locationToIndex(e.getPoint());
					System.out.println("Index: " + index);
					Object element = list.getModel().getElementAt(index);
					String string = (String) element;
					System.out.println("Choice: " + string);
					open(string);
				}
			});
		}
	}
	
	private class BorderLayoutSample extends JPanel {
		public BorderLayoutSample() {
			setLayout(new BorderLayout());
			add(new JButton("Center (temp)"), BorderLayout.CENTER);
			add(new JButton("Center"));
			add(new JButton("North"), BorderLayout.NORTH);
			add(new JButton("South"), BorderLayout.SOUTH);
			add(new JButton("West"), BorderLayout.WEST);
			add(new JButton("East"), BorderLayout.EAST);
		}
	}
	private class BoxLayoutSample extends JPanel {
		public BoxLayoutSample() {
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			for (int i = 0; i < 10; i++) {
				if (i / 2 % 2 == 1) {
					JButton button = new JButton("Maximum Size: Integer.MAX_VALUE");
					button.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
					add(button);
					continue;
				}
				add(new JButton("Button " + i));
			}
		}
	}
	private class CardLayoutSample extends JPanel {
		private CardLayout layout;
		public CardLayoutSample() {
			layout = new CardLayout();
			setLayout(layout);
			for (int i = 0; i < 5; i++) {
				JButton button = new JButton("Card " + i);
				button.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						layout.next(CardLayoutSample.this);
					}
				});
				add(button);
			}
		}
	}
	private class FlowLayoutSample extends JPanel {
		private FlowLayout layout;
		public FlowLayoutSample() {
			layout = new FlowLayout();
			setLayout(layout);
			for (int i = 0; i < 100; i++) {
				if (i == 0) {
					JButton button = new JButton("Add Hgap");
					button.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							int hgap = layout.getHgap();
							layout.setHgap(hgap + 1);
							FlowLayoutSample.this.revalidate();
							System.out.println("Current Hgap: " + layout.getHgap());
						}
					});
					add(button);
					continue;
				} else if (i == 1) {
					JButton button = new JButton("Reduce Hgap");
					button.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							int hgap = layout.getHgap();
							layout.setHgap(hgap - 1);
							FlowLayoutSample.this.revalidate();
							System.out.println("Current Hgap: " + layout.getHgap());
						}
					});
					add(button);
					continue;
				}
				add(new JButton("Button " + i));
			}
		}
	}
	private class GridBagLayoutSample extends JPanel {
		private GridBagLayout layout;
		public GridBagLayoutSample() {
			layout = new GridBagLayout();
			setLayout(layout);
			JButton button;
			GridBagConstraints constraints;
			
			button = new JButton("(0, 0)");
			constraints = new GridBagConstraints();
			constraints.gridx = 0;
			constraints.gridy = 0;
			constraints.weightx = 1;
			constraints.weighty = 1;
			constraints.fill = GridBagConstraints.BOTH;
			layout.setConstraints(button, constraints);
			add(button);
			
			button = new JButton("(1, 1)");
			constraints = new GridBagConstraints();
			constraints.gridx = 1;
			constraints.gridy = 1;
			constraints.weightx = 1;
			constraints.weighty = 1;
			constraints.fill = GridBagConstraints.BOTH;
			layout.setConstraints(button, constraints);
			add(button);
			
			button = new JButton("Width: 2");
			constraints = new GridBagConstraints();
			constraints.gridx = 0;
			constraints.gridy = 2;
			constraints.gridwidth = 2;
			constraints.weightx = 1;
			constraints.weighty = 1;
			constraints.fill = GridBagConstraints.BOTH;
			layout.setConstraints(button, constraints);
			add(button);
		}
	}
	private class GridLayoutSample extends JPanel {
		private GridLayout layout;
		public GridLayoutSample() {
			layout = new GridLayout();
			setLayout(layout);
			int numColumns = 5, numRows = 5;
			layout.setColumns(numColumns);
			layout.setRows(numRows);
			for (int y = 0; y < numRows; y++) {
				for (int x = 0; x < numColumns; x++) {
					add(new JButton("(" + x + ", " + y + ")"));
				}
			}
		}
	}
	private class GroupLayoutSample extends JPanel {
		private GroupLayout layout;
		public GroupLayoutSample() {
			layout = new GroupLayout(this);
			setLayout(layout);
			
			JLabel label00 = new JLabel("Account ID: ");
			JLabel label01 = new JLabel("Account Password: ");
			JTextField field00 = new JTextField("admin");
			JTextField field01 = new JPasswordField("password");
			
			layout.setAutoCreateGaps(true);
			layout.setAutoCreateContainerGaps(true);
			
			SequentialGroup horizontal = layout.createSequentialGroup();
			horizontal.addGroup(layout.createParallelGroup().
					addComponent(label00).
					addComponent(label01));
			horizontal.addGroup(layout.createParallelGroup().
					addComponent(field00).
					addComponent(field01));
			layout.setHorizontalGroup(horizontal);
			
			SequentialGroup vertical = layout.createSequentialGroup();
			vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE).
					addComponent(label00).
					addComponent(field00));
			vertical.addGroup(layout.createParallelGroup(Alignment.BASELINE).
					addComponent(label01).
					addComponent(field01));
			layout.setVerticalGroup(vertical);
		}
	}
	private class OverlayLayoutSample extends JPanel {
		public OverlayLayoutSample() {
			setLayout(new BorderLayout());
			JButton button00 = new JButton("Button 0");
			JButton button01 = new JButton("Button 1");
			button00.setLayout(new OverlayLayout(button00));
			button01.setAlignmentX(1);
			button01.setAlignmentY(1);
			button00.add(button01);
			add(button00);
		}
	}
	private class SpringLayoutSample extends JPanel {
		private SpringLayout layout;
		public SpringLayoutSample() {
			layout = new SpringLayout();
			setLayout(layout);
			
			JButton button00 = new JButton("Button 0");
			add(button00);
			layout.putConstraint(SpringLayout.NORTH, button00, 10, SpringLayout.NORTH, this);
			layout.putConstraint(SpringLayout.WEST, button00, 10, SpringLayout.WEST, this);
			
			JButton button01 = new JButton("Button 1");
			add(button01);
			layout.putConstraint(SpringLayout.NORTH, button01, 10, SpringLayout.SOUTH, button00);
			layout.putConstraint(SpringLayout.WEST, button01, 10, SpringLayout.EAST, button00);
		}
	}
}
